import { observable } from 'mobx';

import service from '@src/service/index';


const showStore = observable({
	title: 'this is home page',
	list: [],
	rightList: [],
	rightWordImgListMap: {},

	// 获取左侧字列表
	GetWordListById(directoryId) {
		var that=this;
		service.GetWordListById(directoryId).then(list=>{
			that.SetRightListDirect(list);
		})
	},

	SetRightListDirect(list) {
		list.forEach(Item => {
			Item['ImgList'] = [];
			Item['TemplateList'] = [];
			Item['TemplateMap'] = {};
		});
		this.rightList = list;
		window.rightList = this.rightList;

		localStorage.setItem('rightList', JSON.stringify(list))

		if (list.length > 0) {
			this.currentWordItem = list[0];
		}
	},
	getRightListDirect() {
		var listJson = localStorage.getItem('rightList') || '';
		if (listJson == "") {
			return [];
		}
		return JSON.parse(listJson);
	},

	// 获取左侧字单字
	GetSingleWordListById(directoryId) {
		var that=this;
		//如果已经取过就不再取了
		if (this.rightWordImgListMap[directoryId] == undefined) {
			service.GetSingleWordListById(directoryId).then(list => {
				that.rightWordImgListMap[directoryId] = list;
				that.currentWordImgList = that.rightWordImgListMap[directoryId];
				console.log(that.rightWordImgListMap, "this.rightWordImgListMap");
			})
		}


	},


	SetCurrentWordItem(item) {
	
		this.currentWordItem = item;

		var directoryId = item.DirectoryID

		//如果已存在  就直接赋值
		if (this.rightWordImgListMap[directoryId]) {
			this.currentWordImgList = this.rightWordImgListMap[directoryId];
		} else {
			this.GetSingleWordListById(directoryId);
		}

	},

	ClearCurrnetWordImgList() {
		this.currentWordImgList = [];
	},

	GetWordItemById(directoryId) {
		var item = this.rightList.find((item) => { return item.DirectoryID = directoryId; })
		console.log(item, "打到字对象");
		return item;
	},

	currentWordItem: null,
	currentWordImgList: null
});

export default showStore;
